package com.jason.kotlin.gank.ui.fragments

import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.jason.kotlin.gank.R
import com.jason.kotlin.gank.model.GankBannerBean
import com.jason.kotlin.gank.model.GankDataBean
import com.jason.kotlin.gank.ui.adapter.GankItemAdapter
import com.jason.kotlin.gank.ui.manage.PageManager
import com.jason.kotlin.gank.ui.section.GankHomeBannerSection
import com.jason.library.common.base.IViewSection
import com.jason.kotlin.gank.viewmodel.GankHomeViewModel
import com.jason.library.common.base.BaseStateFragment
import com.jason.library.common.ext.observe

class GankHomeFragment : BaseStateFragment<GankHomeViewModel>() {

    private lateinit var refreshLayout: SwipeRefreshLayout
    private lateinit var recyclerView: RecyclerView

    private lateinit var section: IViewSection<List<GankBannerBean>>

    private lateinit var itemAdapter: GankItemAdapter

    companion object {

        fun newInstance(): GankHomeFragment {
            return GankHomeFragment()
        }

        inline fun <reified T> getInstance(value: String): T {
            val clz = T::class.java
            val mCreate = clz.getDeclaredConstructor()
            mCreate.isAccessible = true
            return mCreate.newInstance()
        }
    }


    override fun getLayoutId() = R.layout.gank_fragment_home

    override fun getPageTarget() = refreshLayout

    override fun initView(view: View) {
        refreshLayout = view.findViewById(R.id.swipeRefreshLayout)
        recyclerView = view.findViewById(R.id.recyclerView)
        section = GankHomeBannerSection(refreshLayout.context)


        itemAdapter = GankItemAdapter()
        itemAdapter.addHeaderView(section.rootView)


        recyclerView.apply {
            layoutManager = LinearLayoutManager(context)
            setHasFixedSize(true)
            adapter = itemAdapter
        }

        refreshLayout.setOnRefreshListener {
            loadData()
        }

        itemAdapter.setOnItemClickListener { adapter, view, position ->
            run {
                PageManager.openWebPage(
                    requireActivity(),
                    itemAdapter.getItem(position).type,
                    itemAdapter.getItem(position).title,
                    itemAdapter.getItem(position).url
                )
            }
        }
    }


    private fun loadData() {
        refreshLayout.isRefreshing = true
        viewModel!!.loadData()
    }


    override fun onDataInit() {
        loadData()
    }

    /**
     * 绑定LiveData的监听
     */
    override fun bindLiveData() {

        //banner 数据监听
        observe(viewModel!!.bannerData) {
            it!!.data?.let { it1 -> section.setData(it1) }
        }

        observe(viewModel!!.gankData) {
            refreshLayout.isRefreshing = false
            if (it.page > 0) {
                itemAdapter.setNewData(it.data as MutableList<GankDataBean>?)
            } else {
                itemAdapter.addData(it.data!!)
            }
        }


    }

    override fun onStart() {
        super.onStart()
        section.onStart()
    }

    override fun onStop() {
        super.onStop()
        section.onStop()
    }

    override fun reload() {
        onDataInit()
    }


}